package com.happy.unitconvert.common.util;

import java.util.Collection;
import java.util.UUID;

/**
 * 字符串工具类
 */
public class StringUtils extends org.springframework.util.StringUtils {

  /**
   * 生成UUID（去除横线）
   */
  public static String generateUuid() {
    return UUID.randomUUID().toString().replace("-", "");
  }

  /**
   * 生成带横线的UUID
   */
  public static String generateUuidWithHyphen() {
    return UUID.randomUUID().toString();
  }

  /**
   * 判断字符串是否为空或空白
   */
  public static boolean isBlank(String str) {
    return str == null || str.trim().isEmpty();
  }

  /**
   * 判断字符串是否不为空且不为空白
   */
  public static boolean isNotBlank(String str) {
    return !isBlank(str);
  }

  /**
   * 安全的字符串修剪
   */
  public static String safeTrim(String str) {
    return str == null ? null : str.trim();
  }

  /**
   * 安全的字符串转换，null转为空字符串
   */
  public static String safeToString(Object obj) {
    return obj == null ? "" : obj.toString();
  }

  /**
   * 字符串连接
   */
  public static String join(String delimiter, String... elements) {
    if (elements == null || elements.length == 0) {
      return "";
    }
    return String.join(delimiter, elements);
  }

  /**
   * 集合连接为字符串
   */
  public static String join(String delimiter, Collection<String> elements) {
    if (elements == null || elements.isEmpty()) {
      return "";
    }
    return String.join(delimiter, elements);
  }

  /**
   * 首字母大写
   */
  public static String capitalize(String str) {
    if (isBlank(str)) {
      return str;
    }
    return str.substring(0, 1).toUpperCase() + str.substring(1);
  }

  /**
   * 首字母小写
   */
  public static String uncapitalize(String str) {
    if (isBlank(str)) {
      return str;
    }
    return str.substring(0, 1).toLowerCase() + str.substring(1);
  }

  /**
   * 驼峰转下划线
   */
  public static String camelToSnake(String str) {
    if (isBlank(str)) {
      return str;
    }
    return str.replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase();
  }

  /**
   * 下划线转驼峰
   */
  public static String snakeToCamel(String str) {
    if (isBlank(str)) {
      return str;
    }
    StringBuilder result = new StringBuilder();
    boolean upperCase = false;
    for (char c : str.toCharArray()) {
      if (c == '_') {
        upperCase = true;
      } else {
        result.append(upperCase ? Character.toUpperCase(c) : c);
        upperCase = false;
      }
    }
    return result.toString();
  }

  /**
   * 截断字符串
   */
  public static String truncate(String str, int maxLength) {
    if (str == null || str.length() <= maxLength) {
      return str;
    }
    return str.substring(0, maxLength) + "...";
  }

  /**
   * 掩码处理（隐藏中间部分）
   */
  public static String mask(String str, int prefixLen, int suffixLen, String maskChar) {
    if (isBlank(str) || str.length() <= prefixLen + suffixLen) {
      return str;
    }

    String prefix = str.substring(0, prefixLen);
    String suffix = str.substring(str.length() - suffixLen);
    int maskLength = str.length() - prefixLen - suffixLen;

    return prefix + maskChar.repeat(maskLength) + suffix;
  }

  /**
   * 手机号掩码
   */
  public static String maskPhone(String phone) {
    return mask(phone, 3, 4, "*");
  }

  /**
   * 邮箱掩码
   */
  public static String maskEmail(String email) {
    if (isBlank(email) || !email.contains("@")) {
      return email;
    }

    String[] parts = email.split("@");
    String localPart = parts[0];
    String domainPart = parts[1];

    if (localPart.length() <= 2) {
      return email;
    }

    String maskedLocal = mask(localPart, 1, 1, "*");
    return maskedLocal + "@" + domainPart;
  }
}
